/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koor.action.student;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import koor.action.Action;
import koor.action.ActionResult;
import koor.persistent.StudentDAO;
import koor.service.ServiceLocator;

/**
 * The
 * <code>StudentDeleteAction</code> class represents action, used to remove
 * student
 *
 * @author Admin
 */
public class StudentDeleteAction implements Action {

    @Override
    public String getName() {
        return "student.delete";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        StudentDAO studentDAO = ServiceLocator.getService(StudentDAO.class);
        
        if ((request.getParameter("id") != null) && (request.getParameter("name") != null)) {
            try {
                Long id = Long.parseLong(request.getParameter("id"));
                studentDAO.deleteById(id);
                request.getSession().setAttribute("win", request.getParameter("name") + " " + "был удален");
            } catch (NumberFormatException e) {
                request.getSession().setAttribute("fail", "Неверный номер");
            } catch (RuntimeException e) {
                request.getSession().setAttribute("fail", "Студент не найден");
            }
        } else {
            request.getSession().setAttribute("fail", "Студент не был удален");
        }

        if (request.getParameter("search") != null) {
            request.getSession().setAttribute("search", request.getParameter("search"));
        }

        return ActionResult.redirect("student.list");
    }
}
